//
//  UIButton+Extension.swift
//  Weibo10
//
//  Created by male on 15/10/14.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

extension UIButton {
    
    // MARK: - 便利构造函数
    
    /// 创建带图像和背景图像的button
    ///
    /// - Parameters:
    ///   - imageName: 图像名称
    ///   - backImageName: 背景图像名称
    public convenience init(imageName: String?, backImageName: String?) {
        
        self.init()
        guard let imageName = imageName,
              let backImageName = backImageName else {
            return
        }
        
        setImage(UIImage.init(named: imageName), for: .normal)
        setImage(UIImage.init(named: imageName + "_highlighted"), for: .highlighted)
        
        setBackgroundImage(UIImage.init(named: backImageName), for: .normal)
        setBackgroundImage(UIImage.init(named: backImageName + "_highlighted"), for: .highlighted)
        
        sizeToFit()
    }
    
    
    /// 创建带标题、字号大小、文字颜色及高亮颜色
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - fontSize: 字体大小
    ///   - normalColor: 文字颜色
    ///   - highlightColor: 文字高亮颜色 
    public convenience init(title: String, fontSize: CGFloat = 16,
                            normalColor: UIColor, highlightColor: UIColor?) {
        
        self.init()
        guard let highlightColor = highlightColor else {
            return
        }
        setTitle(title, for: .normal)
        setTitleColor(normalColor, for: .normal)
        setTitleColor(highlightColor, for: .highlighted)
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        
        sizeToFit()
    }
    
}
